#include <iostream>
#include <string>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <cstring>
#include <cstdlib>
#include <cerrno>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/stat.h>
#include"Log.hpp"
#include"InetAddr.hpp"
using namespace LogModule;


//模板模式
class Socker{
    public:
        virtual void sockerInit() = 0;
        virtual void sockerStart(int port) = 0;

        void constructer(int port){
            TcpSocker a;
            a.sockerInit();
            a.sockerStart(port);
            
        }

};

class TcpSocker : public Socker{
    public:
    TcpSocker() : _sockfd(-1)
    {

    }
    virtual void sockerInit() override
    {
        _sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if(_sockfd < 0)
        {
            LOG(LogLevel::ERROR) << "create socket failed";
            exit(1);
        }
        sockaddr_in addr;
        InetAddr inetaddr(addr);
        int n = ::bind(_sockfd, inetaddr.NetAddr(),inetaddr.NetAddrLen());
        if(n < 0)
        {
            LOG(LogLevel::ERROR) << "bind socket failed";
            exit(1);
        }
        n = ::listen(_sockfd, 10);
        if(n < 0)
        {
            LOG(LogLevel::ERROR) << "listen socket failed";
            exit(1);
        }
        LOG(LogLevel::INFO) << "create tcp socket success";
    
    }
    virtual void sockerStart(int port) override
    {
        sockaddr_in addr;
        socklen_t addrlen = sizeof(addr);
        while(true)
        {
            int connfd = ::accept(_sockfd, (sockaddr*)&addr, &addrlen);
            
        }
    }
    ~TcpSocker()
    {
        if(_sockfd >= 0)
        {
            ::close(_sockfd);
            _sockfd = -1;
        }
    }
    private:
        int _sockfd;
    };